package cn.wenhe9.novel.controller.front;

import cn.wenhe9.novel.constant.ApiRouterConsts;
import cn.wenhe9.novel.domain.dto.*;
import cn.wenhe9.novel.domain.vo.BookQueryVo;
import cn.wenhe9.novel.result.PageResultDto;
import cn.wenhe9.novel.result.ResultResponse;
import cn.wenhe9.novel.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 小说模块 API 接口
 *
 * @author DuJinliang
 * 2022/5/18
 */
@Api(tags = "小说模块 API 接口")
@RestController
@RequestMapping(ApiRouterConsts.API_FRONT_BOOK_URL_PREFIX)
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 小说分类列表查询接口
     */
    @ApiOperation("小说分类列表查询接口")
    @GetMapping("category/list")
    public ResultResponse<List<BookCategoryDto>> listCategory(Integer workDirection) {
        return bookService.listCategory(workDirection);
    }

    /**
     * 小说搜索接口
     */
    @ApiOperation("小说搜索接口")
    @GetMapping("search_list")
    public ResultResponse<PageResultDto<BookInfoDto>> searchBooks(BookQueryVo condition) {
        return bookService.searchBooks(condition);
    }

    /**
     * 小说信息查询接口
     */
    @ApiOperation("小说信息查询接口")
    @GetMapping("{id}")
    public ResultResponse<BookInfoDto> getBookById(@PathVariable("id") Long bookId) {
        return bookService.getBookById(bookId);
    }

    /**
     * 增加小说点击量接口
     */
    @ApiOperation("增加小说点击量接口")
    @PostMapping("visit")
    public ResultResponse<Void> addVisitCount(Long bookId) {
        return bookService.addVisitCount(bookId);
    }

    /**
     * 小说最新章节相关信息查询接口
     */
    @ApiOperation("小说最新章节相关信息查询接口")
    @GetMapping("last_chapter/about")
    public ResultResponse<BookChapterAboutDto> getLastChapterAbout(Long bookId) {
        return ResultResponse.ok(bookService.getLastChapterAbout(bookId));
    }

    /**
     * 小说推荐列表查询接口
     */
    @ApiOperation("小说推荐列表查询接口")
    @GetMapping("rec_list")
    public ResultResponse<List<BookInfoDto>> listRecBooks(Long bookId) throws NoSuchAlgorithmException {
        return bookService.listRecBooks(bookId);
    }

    /**
     * 小说章节列表查询接口
     */
    @ApiOperation("小说章节列表查询接口")
    @GetMapping("chapter/list")
    public ResultResponse<List<BookChapterDto>> listChapters(Long bookId) {
        return bookService.listChapters(bookId);
    }

    /**
     * 小说内容相关信息查询接口
     */
    @ApiOperation("小说内容相关信息查询接口")
    @GetMapping("content/{chapterId}")
    public ResultResponse<BookContentAboutDto> getBookContentAbout(@PathVariable("chapterId") Long chapterId) {
        return ResultResponse.ok(bookService.getBookContentAbout(chapterId));
    }

    /**
     * 获取上一章节ID接口
     */
    @ApiOperation("获取上一章节ID接口")
    @GetMapping("pre_chapter_id/{chapterId}")
    public ResultResponse<Long> getPreChapterId(@PathVariable("chapterId") Long chapterId) {
        return bookService.getPreChapterId(chapterId);
    }

    /**
     * 获取下一章节ID接口
     */
    @ApiOperation("获取下一章节ID接口")
    @GetMapping("next_chapter_id/{chapterId}")
    public ResultResponse<Long> nextChapterId(@PathVariable("chapterId") Long chapterId) {
        return bookService.getNextChapterId(chapterId);
    }

    /**
     * 小说点击榜查询接口
     */
    @ApiOperation("小说点击榜查询接口")
    @GetMapping("visit_rank")
    public ResultResponse<List<BookRankDto>> listVisitRankBooks() {
        return bookService.listVisitRankBooks();
    }

    /**
     * 小说新书榜查询接口
     */
    @ApiOperation("小说新书榜查询接口")
    @GetMapping("newest_rank")
    public ResultResponse<List<BookRankDto>> listNewestRankBooks() {
        return bookService.listNewestRankBooks();
    }

    /**
     * 小说更新榜查询接口
     */
    @ApiOperation("小说更新榜查询接口")
    @GetMapping("update_rank")
    public ResultResponse<List<BookRankDto>> listUpdateRankBooks() {
        return bookService.listUpdateRankBooks();
    }

    /**
     * 小说最新评论查询接口
     */
    @ApiOperation("小说最新评论查询接口")
    @GetMapping("comment/newest_list")
    public ResultResponse<BookCommentDto> listNewestComments(Long bookId) {
        return bookService.listNewestComments(bookId);
    }
}
